<?php

class ProfileDAO extends DAO{


	function getById($id){
		try
		{
			$id = Validator::escapeSql($id);
			$query="select * from PROFILE where ID=$id;";
			 
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$TokenDAO = $this->factory->getTokenDAO();
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = new Profile((int)$Bean->ID);
				$thebean->setLabel($Bean->LABEL);
				$thebean->setComment($Bean->COMMENT);
				$thebean->setAvailable($Bean->AVAILABLE);
				$thebean->setTokens($TokenDAO->getByProfile($thebean));
			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $thebean;
	}
	function getByLabel($labelstr){
		try
		{
			$labelstr = Validator::escapeSql($labelstr);
			$query="select * from PROFILE where LABEL='$labelstr';";

			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$TokenDAO = $this->factory->getTokenDAO();
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = new Profile((int)$Bean->ID);
				$thebean->setLabel($Bean->LABEL);
				$thebean->setComment($Bean->COMMENT);
				$thebean->setAvailable($Bean->AVAILABLE);
				$thebean->setTokens($TokenDAO->getByProfile($thebean));
			}
		}
		catch(Exception $e)     {
			throw $e;
		}
		 
		return $thebean;
		 
	}
	function remove($Bean){
		try{
			 
			$query="update PROFILE set AVAILABLE = false where ID=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}catch(Exception $e)    {
			$bool=false;
		}
		return $bool;

	}
	function update($Bean){
		try{
			$query = "update PROFILE set LABEL='".Validator::escapeSql($Bean->getLabel())."',COMMENT='".Validator::escapeSql($Bean->getComment())."',AVAILABLE =".Validator::escapeSql($Bean->getAvailable())." where ID=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$tokenDAO = $this->factory->getTokenDAO();
			foreach ($Bean->getTokens() as $token){
				if(is_null($token->getId())){
					$token = $tokenDAO->add($token);
					//$fkQuery = "insert into PROFILE_FK_TOKEN_PACK (profile_id,token_pack_id) values (".$Bean->id.",".$tokenPack->getId().")";
					$this->pdo->exec($fkQuery);
				}else{
					$tokenDAO->update($token);
				}
			}
			$bool=true;
		}
		catch(Exception $e)     {
			$bool=false;
			 
		}
		return $bool;
	}
	function add($Bean){

		try{
			if (is_null($Bean->getId())){
				$insertQuery = "insert PROFILE (LABEL,COMMENT,AVAILABLE) values ('".Validator::escapeSql($Bean->getLabel())."','".Validator::escapeSql($Bean->getComment())."',".Validator::escapeSql($Bean->getAvailable()).");";
				 
				$this->pdo->exec($insertQuery);
				$thebean = null;
				$thebean = $this->getById((int)$this->pdo->lastInsertId());
				$tokenDAO = $this->factory->getTokenDAO();
				foreach ($Bean->getTokens() as $token){
					if(is_null($token->getId())){
						$token = $tokenDAO->add($token);
					}else{
						$tokenDAO->update($token);
						 
					}
				}
			}else{
				$this->update($Bean);
			}
		}catch(Exception $e)    {$this->pdo->message;}
		return $thebean;
	}

}